package com.example.game.console.controller;

import com.example.game.console.domain.CategoryTreeVO;
import com.example.game.module.entity.Category;
import com.example.game.module.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")
public class ConsoleCategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/tree")
    public Map<String, Object> getCategoryTree() {
        List<Category> categories = categoryService.findAll();

        // 构建类目树
        List<CategoryTreeVO> categoryTree = buildCategoryTree(categories);

        // 构建响应
        Map<String, Object> response = new HashMap<>();
        response.put("categoryTree", categoryTree);

        return response;
    }

    // 递归构建类目树
    private List<CategoryTreeVO> buildCategoryTree(List<Category> categories) {
        return categories.stream()
                .filter(c -> c.getParentId() == null) // 筛选顶级类目
                .map(parent -> {
                    CategoryTreeVO parentVO = new CategoryTreeVO();
                    parentVO.setId(parent.getId());
                    parentVO.setName(parent.getName());
                    parentVO.setImage(parent.getImage());

                    // 递归构建子类目
                    List<CategoryTreeVO> children = buildSubCategories(categories, parent.getId());
                    parentVO.setChildren(children);

                    return parentVO;
                })
                .collect(Collectors.toList());
    }

    // 递归构建子类目
    private List<CategoryTreeVO> buildSubCategories(List<Category> categories, Long parentId) {
        return categories.stream()
                .filter(c -> parentId != null && parentId.equals(c.getParentId()))
                .map(child -> {
                    CategoryTreeVO childVO = new CategoryTreeVO();
                    childVO.setId(child.getId());
                    childVO.setName(child.getName());
                    childVO.setImage(child.getImage());

                    // 递归构建孙类目
                    List<CategoryTreeVO> grandchildren = buildSubCategories(categories, child.getId());
                    childVO.setChildren(grandchildren);

                    return childVO;
                })
                .collect(Collectors.toList());
    }



    @PostMapping("/add")
    public String addCategory(@RequestParam(name = "categoryId",required = false)Long categoryId,
                              @RequestParam(name = "name")String name,
                              @RequestParam(name = "image")String image) {
        try {
            Long resultCategoryId = categoryService.edit(categoryId, name, image);
            return resultCategoryId.toString();
        }catch (Exception e) {
            return e.getMessage();
        }
    }

    @PostMapping("/update")
    public String updateCategory(@RequestParam(name = "categoryId")Long categoryId,
                                 @RequestParam(name = "name")String name,
                                 @RequestParam(name = "image")String image) {
        try {
            Long resultCategoryId = categoryService.edit(categoryId, name, image);
            return resultCategoryId.toString();
        }catch (Exception e) {
            return e.getMessage();
        }
    }


    private List<CategoryTreeVO> convertToTreeVO(List<Category> categories) {
        return categories.stream().map(this::convertToTreeVO).collect(Collectors.toList());
    }

    private CategoryTreeVO convertToTreeVO(Category category) {
        CategoryTreeVO vo = new CategoryTreeVO();
        vo.setId(category.getId());
        vo.setName(category.getName());
        vo.setImage(category.getImage());
        List<Category> children = categoryService.getChildren(category.getId());
        if (!children.isEmpty()) {
            vo.setChildren(convertToTreeVO(children));
        }
        return vo;
    }

}
